home *** CD-ROM | disk | FTP | other *** search
/ Gigarom 1 / Gigarom Macintosh Archives (Quantum Leap)(CDRM1080320)(1993).iso / FILES / DEV / C-H / CHexDmpDA.cpt / Hex Dump DA / window.HexDumpDA.c < prev   
C/C++ Source or Header  |  1988-06-14  |  10KB  |  500 lines

  1. #include <QuickDraw.h>
  2. #include <WindowMgr.h>
  3. #include <ControlMgr.h>
  4. #include <FontMgr.h>
  5. #include <DeviceMgr.h>
  6. #include <FileMgr.h>
  7. #include <SetUpA4.h>
  8.  
  9. #include "HexDump.h"
  10.  
  11. extern    char         buffer[];
  12. extern    WindowPtr     wp;
  13. extern    long        fileSize;
  14. extern    DCtlPtr     dce;                    /*  device control entry  */
  15. extern    enum fork    whichFork;
  16.  
  17. ControlHandle         vScroll = (ControlHandle) 0L;
  18. int                    curTop = 0;
  19. int                 lineHeight;
  20. int                    nLines;
  21. long                 firstByte = -1L;
  22. long                linesInFile;
  23. long                 sBegin    =    -1;
  24. long                 sEnd    =    -1;
  25.  
  26. FontInfo     fInfo;
  27.  
  28. #define TOPH    12
  29. #define TOPV    3
  30.  
  31. long abs(i)
  32. long i;
  33. {
  34.     if (i<0) return(-i);
  35.     else return(i);
  36. }
  37.  
  38. Scroll_Bits()
  39. {
  40.     int            oldTop;
  41.     RgnHandle    tmpRgn, saveRgn;
  42.     long        dv;
  43.     Rect        r;
  44.     
  45.     oldTop = curTop;
  46.     curTop = GetCtlValue( vScroll );
  47.     dv = (long)lineHeight * (oldTop-curTop);
  48.     
  49.     r = wp->portRect;
  50.     r.left += TOPH;
  51.     r.top += TOPV + 2 + lineHeight;
  52.     r.right -= 16;
  53.     r.bottom = r.top + nLines*lineHeight;
  54.     tmpRgn=NewRgn();
  55.     saveRgn = NewRgn();
  56.     CopyRgn(wp->clipRgn, saveRgn);
  57.     if (dv) {
  58.         if (abs(dv)>=(long)lineHeight*nLines) {
  59.             EraseRect( &r );
  60.             RectRgn( tmpRgn, &r );
  61.         }
  62.         else {
  63.             saveRgn = NewRgn();
  64.             CopyRgn(wp->clipRgn, saveRgn);
  65.             ClipRect(&r);
  66.             ScrollRect( &r, 0, dv, tmpRgn );
  67.             SetClip(saveRgn);
  68.             DisposeRgn(saveRgn);
  69.         }
  70.         InvalRgn( tmpRgn );
  71.     }
  72.     DisposeRgn( tmpRgn );
  73. }
  74.  
  75. max(a,b)
  76. {
  77.     if (a>b) return(a);
  78.     else return(b);
  79. }
  80.  
  81. /*
  82.  *    doUpdate - draw our window's contents.
  83.  *
  84.  */
  85.  
  86. doUpdate()
  87. {
  88.     GrafPtr savePort;
  89.     Point     p;
  90.     long     first;                    /* offset of the first byte of "buffer" */
  91.     register int     i;
  92.     RgnHandle    tmpRgn;
  93.     Rect    r, testRect, dummyRect;
  94.     char    display[128];
  95.     
  96.     GetPort( &savePort );
  97.     SetPort( wp );
  98.     
  99.     nLines = ((wp->portRect.bottom - wp->portRect.top - TOPV - 17)/lineHeight) - 1;
  100.     SetCtlMax( vScroll, max( 0, (int)(linesInFile-nLines)) );
  101.     ReadFrom(first = 16L*GetCtlValue( vScroll ));
  102.     
  103.     Scroll_Bits();
  104.         
  105.     BeginUpdate( wp );
  106.     EraseRect(&wp->portRect);
  107.     DrawGrowIcon( wp );
  108.     DrawControls( wp );
  109.  
  110.     /* save, then adjust the ClipRgn */
  111.     tmpRgn = NewRgn();
  112.     GetClip( tmpRgn );
  113.     r = wp->portRect;
  114.     r.right -= 15;
  115.     r.bottom -= 16;
  116.     ClipRect(&r);
  117.  
  118.     Draw_Window_Header();
  119.     
  120.     r.right -= 1;
  121.     ClipRect(&r);
  122.     
  123.     TextFace(0);
  124.     MoveTo( TOPH, TOPV+2*lineHeight );
  125.     for (i=0; i<nLines; i++) {
  126.         /* check to see if line needs updating...*/
  127.         GetPen( &p );
  128.         SetRect(&testRect, p.h, p.v-fInfo.ascent, 
  129.                     wp->portRect.right, p.v+fInfo.descent);
  130.         if (RectInRgn( &testRect, wp->visRgn)) {
  131.             Fill_Line(first, &buffer[first-firstByte], display);
  132.             DrawString(display);
  133.         }
  134.         if (sBegin>0) Check_Select(first, true);
  135.         MoveTo( p.h, p.v+lineHeight );
  136.         first += 16;
  137.     }
  138.     
  139.     /* put everything back */
  140.     SetClip(tmpRgn);
  141.     DisposeRgn(tmpRgn);
  142.     
  143.     EndUpdate( wp );
  144.     
  145.     SetPort( savePort );
  146. }
  147.  
  148. New_Select(begin, end)
  149. long begin, end;
  150. {
  151.     GrafPtr savePort;
  152.     Point     p;
  153.     long     first = 16L*GetCtlValue( vScroll );
  154.     register int     i;
  155.     RgnHandle    tmpRgn;
  156.     Rect    r;
  157.     
  158.     GetPort( &savePort );
  159.     SetPort( wp );
  160.     first = 16L*GetCtlValue( vScroll );
  161.     nLines = ((wp->portRect.bottom - wp->portRect.top - TOPV - 17)/lineHeight) - 1;
  162.     
  163.     /* save, then adjust the ClipRgn */
  164.     tmpRgn = NewRgn();
  165.     GetClip( tmpRgn );
  166.     r = wp->portRect;
  167.     r.right -= 16;
  168.     r.bottom -= 16;
  169.     ClipRect(&r);
  170.     
  171.     MoveTo( TOPH, TOPV+2*lineHeight );
  172.     for (i=0; i<nLines; i++) {
  173.         /* check to see if selection is on this line...*/
  174.         GetPen( &p );
  175.         Check_Select(first, true);
  176.         MoveTo( p.h, p.v+lineHeight );
  177.         first += 16;
  178.     }
  179.     
  180.     sBegin = begin;
  181.     sEnd = end;
  182.  
  183.     first = 16L*GetCtlValue( vScroll );
  184.     MoveTo( TOPH, TOPV+2*lineHeight );
  185.     for (i=0; i<nLines; i++) {
  186.         /* check to see if selection is on this line...*/
  187.         GetPen( &p );
  188.         Check_Select(first, false);
  189.         MoveTo( p.h, p.v+lineHeight );
  190.         first += 16;
  191.     }
  192.     
  193.     /* put everything back */
  194.     SetClip(tmpRgn);
  195.     DisposeRgn(tmpRgn);
  196.  
  197.     SetPort( savePort );
  198. }
  199.  
  200. Show_Point(n)
  201. long n;
  202. {
  203.     int         new;
  204.     long        delta;
  205.     GrafPtr        savePort;
  206.     int         max;
  207.     
  208.     delta = (new=n/16) - GetCtlValue(vScroll);
  209.     if ((delta<0) || (delta>=nLines)) {
  210.         new -= nLines/2;
  211.         if (new<0) new = 0;
  212.         else if (new>(max=GetCtlMax(vScroll))) new = max;
  213.         SetCtlValue(vScroll, new);
  214.         GetPort(&savePort);
  215.         SetPort(wp);
  216.         InvalRect(&wp->portRect);
  217.         SetPort(savePort);
  218.     }
  219. }
  220.  
  221. Draw_Window_Header()
  222. {
  223.     Str255 s;
  224.     
  225.     MoveTo( TOPH, TOPV+lineHeight-2 );
  226.     TextFace(bold);
  227.     NumToString( fileSize, s );
  228.     DrawString("\pEOF = ");
  229.     DrawString(s);
  230.     DrawString("\p ($");
  231.     Num_To_Hex( fileSize, s );
  232.     DrawString(s);
  233.     DrawString("\p)");
  234.     if (whichFork==data) pstrcpy(s, "\pData fork");
  235.     else pstrcpy(s, "\pResource fork");
  236.     MoveTo( wp->portRect.right-23-StringWidth(s), TOPV+lineHeight-2 );
  237.     DrawString(s);
  238.     MoveTo( wp->portRect.right-15, TOPV+lineHeight );
  239.     LineTo( wp->portRect.left, TOPV+lineHeight );
  240. }
  241.  
  242. static beforeCoord1[16] = { 9, 11, 14, 16, 19, 21, 24, 26,
  243.                             30, 32, 35, 37, 40, 42, 45, 47 };
  244. static afterCoord1[16]    = { 11, 13, 16, 18, 21, 23, 26, 28,
  245.                             32, 34, 37, 39, 42, 44, 47, 49 };
  246. #define before2        52
  247.  
  248. Check_Select(first, drawit)
  249. long first;
  250. Boolean drawit;
  251. {
  252.     register int w = CharWidth('0');
  253.     Point p;
  254.     Rect r;
  255.     int left1, left2, right1, right2;
  256.     
  257.     if (sBegin<first) {
  258.         if (sEnd<=first) return;
  259.         left1 = TOPH + 9*w;
  260.         left2 = TOPH + before2*w;
  261.     }
  262.     else if (sBegin<first+16) {
  263.         left1 = TOPH + beforeCoord1[sBegin-first]*w;
  264.         left2 = TOPH + (before2 + sBegin - first)*w;
  265.     }
  266.     else return;
  267.     
  268.     if (sEnd<first+16) {
  269.         right1 = TOPH + afterCoord1[sEnd-first-1]*w;
  270.         right2 = TOPH + (before2 + sEnd - first)*w;
  271.     }
  272.     else {
  273.         right1 = TOPH + afterCoord1[15]*w;
  274.         right2 = TOPH + (before2+16)*w;
  275.     }
  276.     GetPen(&p);
  277.     SetRect(&r, left1-1, p.v-fInfo.ascent, right1, p.v+fInfo.descent);
  278.     if (drawit) InvertRect(&r);
  279.     else InvalRect(&r);
  280.     SetRect(&r, left2-1, p.v-fInfo.ascent, right2, p.v+fInfo.descent);
  281.     if (drawit) InvertRect(&r);
  282.     else InvalRect(&r);
  283. }
  284.  
  285. /*
  286.  *    ScrollProc - the call-back routine to handle scrolling
  287.  *
  288.  *    This routine is called from the OS routine TrackControl.  As such,
  289.  *    it must be declared "pascal".  It also references data global to
  290.  *    the DA, and thus must SetUpA4 before updating the window's
  291.  *    appearance.  Finally, it return to TrackControl, so it must
  292.  *    RestoreA4 before going back.
  293.  */
  294.  
  295. int scrollCode;
  296. int scrollAmt;
  297.  
  298. pascal void ScrollProc(theControl, theCode)
  299. ControlHandle    theControl;
  300. int                theCode;
  301. {
  302.     int locVal;
  303.     GrafPtr theWindow;
  304.     
  305.     SetUpA4();
  306.     if (theCode==scrollCode) {
  307.         locVal = GetCtlValue(theControl);
  308.         SetCtlValue( theControl, locVal+scrollAmt );
  309.         doUpdate();
  310.     }
  311.     RestoreA4();
  312. }
  313.  
  314. doScrollControl(p)
  315. Point p;
  316. {
  317.     GrafPtr savePort;
  318.     int code,v;
  319.     ControlHandle theControl;
  320.     
  321.     RememberA4();
  322.  
  323.     GetPort( &savePort );
  324.     SetPort( wp );
  325.  
  326.     GlobalToLocal(&p);
  327.     switch ( code = FindControl(p, wp, &theControl) ) {
  328.         case inUpButton:     scrollAmt = -1;         break;
  329.         case inDownButton:     scrollAmt = 1;             break;
  330.         case inPageUp:        scrollAmt = -nLines+1;     break;
  331.         case inPageDown:    scrollAmt = nLines-1;     break;
  332.         default: ;
  333.     }
  334.     if (theControl==vScroll) {
  335.         switch (code) {
  336.             case inUpButton:
  337.             case inDownButton:
  338.             case inPageUp:
  339.             case inPageDown:
  340.                 scrollCode = code;
  341.                 if (TrackControl(vScroll, p, &ScrollProc)) ;
  342.                 break;
  343.             case inThumb:
  344.                 if (TrackControl(theControl, p, 0L )) ;
  345.                 doUpdate();
  346.                 break;
  347.             default: ;
  348.         }
  349.     }
  350.     SetPort( savePort );
  351. }
  352.  
  353.  
  354. /* Activate the DA's window */
  355.  
  356. doActivate(activate)
  357. Boolean activate;
  358. {
  359.     Rect r;
  360.     GrafPtr    savePort;
  361.     
  362.     GetPort(&savePort);
  363.     SetPort(wp);
  364.     r = wp->portRect;
  365.     r.top = r.bottom - 16;
  366.     r.left = r.left - 16;
  367.     InvalRect(&r);
  368.     if ( activate ) ShowControl( vScroll );
  369.     else HideControl( vScroll );
  370.     SetPort(savePort);
  371. }
  372.  
  373. doGrow(p)
  374. Point p;
  375. {
  376.     GrafPtr    savePort;
  377.     long    theResult;
  378.     Point    thePt;
  379.     Rect     r;
  380.     static Rect    wBounds = { 80, 300, 32767, 444 };
  381.     
  382.     GetPort( &savePort );
  383.     SetPort( wp );
  384.  
  385.     theResult = GrowWindow( wp, p, &wBounds );
  386.     thePt.h = LoWord( theResult );
  387.     thePt.v = HiWord( theResult );
  388.     thePt.v = lineHeight*(thePt.v/lineHeight);
  389.     r = wp->portRect;
  390.     r.left = r.right-16;
  391.     InvalRect(&r);
  392.     r.left = wp->portRect.left;
  393.     r.top = r.bottom-16;
  394.     InvalRect(&r);
  395.     SizeWindow( wp, thePt.h, thePt.v, 1 );
  396.     r = wp->portRect;
  397.     r.left = r.right-16;
  398.     MoveControl( vScroll, r.left+1, r.top-1);
  399.     SizeControl( vScroll, 16, r.bottom-r.top-13);
  400.     InvalRect(&r);
  401.     r.left = wp->portRect.left;
  402.     r.top = r.bottom-16;
  403.     InvalRect(&r);
  404.     r = wp->portRect;
  405.     r.bottom = r.top + TOPV + lineHeight-2;
  406.     InvalRect(&r);
  407.  
  408.     SetPort( savePort );
  409. }
  410.  
  411. doZoom(p,msg)
  412. Point p;
  413. int msg;
  414. {
  415.     GrafPtr savePort;
  416.     Rect r;
  417.     
  418.     GetPort(&savePort);
  419.     SetPort(wp);
  420.     if (TrackBox(wp,p,msg)) {
  421.         EraseRect(&wp->portRect);
  422.         ZoomWindow(wp,msg,false);
  423.         r = wp->portRect;
  424.         r.left = r.right-16;
  425.         MoveControl( vScroll, r.left+1, r.top-1);
  426.         SizeControl( vScroll, 16, r.bottom-r.top-13);
  427.     }
  428.     SetPort(savePort);
  429. }
  430.  
  431. /* New_File_Window - open the window on a new file */
  432. New_File_Window(size, name)
  433. long size;
  434. char *name;
  435. {
  436.     GrafPtr    savePort;
  437.  
  438.     linesInFile = (fileSize-1)/16 + 1;
  439.     firstByte = -1;                /* to force a re-read */
  440.  
  441.     GetPort(&savePort);
  442.     SetWTitle( wp, name );
  443.     ShowWindow( wp );
  444.     SelectWindow( wp );
  445.     SetPort(wp);
  446.     InvalRect( &wp->portRect );
  447.     SetCtlValue(vScroll, 0);
  448.     SetPort(savePort);
  449. }
  450.  
  451. /* Create_Window - create our HexDump window */
  452.  
  453. #define WINDOWWIDTH     444
  454. #define WINDOWHEIGHT     276
  455. #define WINDOWTOP        40
  456. #define WINDOWLEFT        10
  457. #define WINDOWBOTTOM    WINDOWTOP + WINDOWHEIGHT
  458. #define WINDOWRIGHT        WINDOWLEFT + WINDOWWIDTH
  459. #define SBARWIDTH         16
  460. #define SBARLEFT        WINDOWWIDTH - SBARWIDTH + 1
  461. #define SBARRIGHT        WINDOWWIDTH + 1
  462. #define SBARBOTTOM        WINDOWHEIGHT - SBARWIDTH + 2
  463.  
  464. Create_Window()
  465. {
  466.     GrafPtr    savePort;
  467.     static     Rect    bounds = {     WINDOWTOP, WINDOWLEFT, 
  468.                                 WINDOWBOTTOM, WINDOWRIGHT },
  469.                     scrollBounds = { -1, SBARLEFT,
  470.                                      SBARBOTTOM, SBARRIGHT };
  471.  
  472.     GetPort(&savePort);
  473.     wp = NewWindow( 0L, &bounds, "", FALSE, 8, 0L, TRUE, 0L );
  474.     
  475.     /* IMPORTANT - set the windowkind to the dCtlRefNum */
  476.     ((WindowPeek)wp)->windowKind = dce->dCtlRefNum;
  477.     dce->dCtlWindow = wp;                            /* courtesy of TLL */
  478.  
  479.     /* set up the window's font and line height: */
  480.     SetPort(wp);
  481.     TextFont(monaco);
  482.     TextSize(9);
  483.     GetFontInfo(&fInfo);
  484.     lineHeight = fInfo.ascent+fInfo.descent+fInfo.leading;
  485.     
  486.     /* set up the scroll bar */
  487.     vScroll = NewControl( wp, &scrollBounds, "", true, 0, 0, 0, 
  488.                 scrollBarProc, 0L );
  489.     SetPort(savePort);
  490. }
  491.  
  492. Destroy_Window()
  493. {
  494.     if (vScroll) DisposeControl( vScroll );
  495.     if (wp) {
  496.         DisposeWindow( wp );
  497.         wp = 0L;
  498.         dce->dCtlWindow = 0;    /* courtesy of TLL */
  499.     }
  500. }